//! 宏定义模块
//! 
//! 提供Yaya库的常用宏定义

/// 断言宏（调试版本）
#[macro_export]
macro_rules! yaya_assert {
    ($condition:expr) => {
        if !$condition {
            panic!("断言失败: {}", stringify!($condition));
        }
    };
    ($condition:expr, $($arg:tt)*) => {
        if !$condition {
            panic!($($arg)*);
        }
    };
}

/// 调试断言宏（仅调试版本有效）
#[macro_export]
macro_rules! yaya_debug_assert {
    ($($arg:tt)*) => {
        #[cfg(debug_assertions)]
        yaya_assert!($($arg)*);
    };
}

/// 检查结果并返回错误的宏
#[macro_export]
macro_rules! yaya_try {
    ($expr:expr) => {
        match $expr {
            Ok(val) => val,
            Err(err) => return Err(err.into()),
        }
    };
}

/// 创建静态变量的宏
#[macro_export]
macro_rules! yaya_static {
    ($name:ident: $ty:ty = $expr:expr) => {
        static $name: ::core::sync::atomic::AtomicPtr<$ty> = 
            ::core::sync::atomic::AtomicPtr::new(::core::ptr::null_mut());
        
        #[allow(dead_code)]
        fn init_$name() -> &'static mut $ty {
            unsafe {
                let ptr = $name.load(::core::sync::atomic::Ordering::Acquire);
                if ptr.is_null() {
                    let boxed = ::alloc::boxed::Box::new($expr);
                    let raw = ::alloc::boxed::Box::into_raw(boxed);
                    if $name.compare_exchange(
                        ::core::ptr::null_mut(), 
                        raw, 
                        ::core::sync::atomic::Ordering::AcqRel, 
                        ::core::sync::atomic::Ordering::Acquire
                    ).is_ok() {
                        &mut *raw
                    } else {
                        let _ = ::alloc::boxed::Box::from_raw(raw);
                        &mut *$name.load(::core::sync::atomic::Ordering::Acquire)
                    }
                } else {
                    &mut *ptr
                }
            }
        }
        
        #[allow(dead_code)]
        fn get_$name() -> &'static mut $ty {
            unsafe {
                &mut *$name.load(::core::sync::atomic::Ordering::Acquire)
            }
        }
    };
}

/// 位操作宏
#[macro_export]
macro_rules! yaya_bit {
    ($n:expr) => {
        1 << $n
    };
}

/// 位域设置宏
#[macro_export]
macro_rules! yaya_set_bit {
    ($reg:expr, $bit:expr) => {
        $reg |= yaya_bit!($bit)
    };
}

/// 位域清除宏
#[macro_export]
macro_rules! yaya_clear_bit {
    ($reg:expr, $bit:expr) => {
        $reg &= !yaya_bit!($bit)
    };
}

/// 位域测试宏
#[macro_export]
macro_rules! yaya_test_bit {
    ($reg:expr, $bit:expr) => {
        ($reg & yaya_bit!($bit)) != 0
    };
}

/// 对齐宏
#[macro_export]
macro_rules! yaya_align_up {
    ($value:expr, $alignment:expr) => {
        ($value + $alignment - 1) & !($alignment - 1)
    };
}

/// 向下对齐宏
#[macro_export]
macro_rules! yaya_align_down {
    ($value:expr, $alignment:expr) => {
        $value & !($alignment - 1)
    };
}

/// 检查对齐宏
#[macro_export]
macro_rules! yaya_is_aligned {
    ($value:expr, $alignment:expr) => {
        ($value & ($alignment - 1)) == 0
    };
}